package com.blacksea.resourceserver.web.security.login;

import com.blacksea.resourceserver.web.security.response.OAuth2CommonResponse;
import com.blacksea.resourceserver.web.security.response.ResponseStateEnum;
import com.blacksea.resourceserver.web.security.util.ResponseUtilObject;

import lombok.SneakyThrows;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 未认证入口, 未登录访问要登录的内容
 *
 * @author blacksea3(jxt) 2021/2/6 23:05
 */
@Component(value = "unAuthenticationEntryPoint")
public class UnAuthenticationEntryPoint implements AuthenticationEntryPoint {

    @Autowired
    private ResponseUtilObject responseUtilObject;

    @SneakyThrows
    @Override
    public void commence(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
        responseUtilObject.outJson(httpServletResponse,
            new OAuth2CommonResponse(ResponseStateEnum.FAIL, "token invalid or miss")
        );
    }
}
